package managedbean.Production;

import entity.ProductionSchedule;
import entity.ProductionScheduleDay;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.ejb.EJB;
import javax.faces.bean.ManagedProperty;
import javax.faces.event.ActionEvent;
import managedbean.UserSessionManagerBean;
import org.primefaces.event.DateSelectEvent;
import session.stateless.ProductionScheduleSessionBeanLocal;

@ManagedBean(name = "productionInfoManagerBean")
@RequestScoped
public class ProductionInfoManagerBean {

    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    @EJB
    private ProductionScheduleSessionBeanLocal productionScheduleSessionBeanLocal;


    public ProductionInfoManagerBean() {
    }
    private Date date1;
    private DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    private String loadProductionInfo;


    public void handleDateSelect(DateSelectEvent event) {

        String dateOfFc = df.format(event.getDate());
            String str[] = dateOfFc.split("-");

            ArrayList<ProductionScheduleDay> productionScheduleDays = new ArrayList <ProductionScheduleDay> ();
            ArrayList<ProductionSchedule> pss = productionScheduleSessionBeanLocal.getProductionSchedules(Integer.parseInt(str[1]), Integer.parseInt(str[2]));
            for (ProductionSchedule ps : pss) {
                for (ProductionScheduleDay psd : ps.getProductionScheduleDays()) {
                    if (psd.getCurrentDay().compareTo(Integer.parseInt(str[0])) ==0) 
                    {            
                        productionScheduleDays.add(psd);
                    }
                }

            }
     
            userSessionManagerBean.setSelectedDay (dateOfFc);
        userSessionManagerBean.setProductionInfoDays(productionScheduleDays);
        
    }

    /**
     * @return the date
     */
    public Date getDate1() {
        return date1;
    }

    /**
     * @param date the date to set
     */
    public void setDate1(Date date1) {
        this.date1 = date1;
    }

    
    /**
     * @return the userSessionManagerBean
     */
    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    /**
     * @param userSessionManagerBean the userSessionManagerBean to set
     */
    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }
    
    public void prev (ActionEvent event)
    {
       
        try 
        {
        Date date = df.parse((String)userSessionManagerBean.getSelectedDay());
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(cal.DAY_OF_MONTH, -1);
        date = cal.getTime();
        String temp = df.format(date);
        
          String str[] = temp.split("-");

            ArrayList<ProductionScheduleDay> productionScheduleDays = new ArrayList <ProductionScheduleDay> ();
            ArrayList<ProductionSchedule> pss = productionScheduleSessionBeanLocal.getProductionSchedules(Integer.parseInt(str[1]), Integer.parseInt(str[2]));
            for (ProductionSchedule ps : pss) {
                for (ProductionScheduleDay psd : ps.getProductionScheduleDays()) {
                    if (psd.getCurrentDay().compareTo(Integer.parseInt(str[0])) ==0) 
                    {            
                        productionScheduleDays.add(psd);
                    }
                }

            }
     
            userSessionManagerBean.setSelectedDay (temp );
        userSessionManagerBean.setProductionInfoDays(productionScheduleDays);
        }catch (Exception ex){}
    }
    
    public void next (ActionEvent event)
    {
         try 
        {
        Date date = df.parse((String)userSessionManagerBean.getSelectedDay());
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(cal.DAY_OF_MONTH, 1);
        date = cal.getTime();
        String temp = df.format(date);
        
          String str[] = temp.split("-");

            ArrayList<ProductionScheduleDay> productionScheduleDays = new ArrayList <ProductionScheduleDay> ();
            ArrayList<ProductionSchedule> pss = productionScheduleSessionBeanLocal.getProductionSchedules(Integer.parseInt(str[1]), Integer.parseInt(str[2]));
            for (ProductionSchedule ps : pss) {
                for (ProductionScheduleDay psd : ps.getProductionScheduleDays()) {
                    if (psd.getCurrentDay().compareTo(Integer.parseInt(str[0])) ==0) 
                    {            
                        productionScheduleDays.add(psd);
                    }
                }

            }
     
            userSessionManagerBean.setSelectedDay (temp );
        userSessionManagerBean.setProductionInfoDays(productionScheduleDays);
        }catch (Exception ex){}
    }

    /**
     * @return the loadProductionInfo
     */
    public String getLoadProductionInfo() {
        
        if (userSessionManagerBean.getProductionInfoDays() ==null)
        {
         String dateOfFc = df.format(Calendar.getInstance().getTime());
            String str[] = dateOfFc.split("-");

            ArrayList<ProductionScheduleDay> productionScheduleDays = new ArrayList <ProductionScheduleDay> ();
            ArrayList<ProductionSchedule> pss = productionScheduleSessionBeanLocal.getProductionSchedules(Integer.parseInt(str[1]), Integer.parseInt(str[2]));
            for (ProductionSchedule ps : pss) {
                for (ProductionScheduleDay psd : ps.getProductionScheduleDays()) {
                    if (psd.getCurrentDay().compareTo(Integer.parseInt(str[0])) ==0) 
                    {            
                        productionScheduleDays.add(psd);
                    }
                }

            }
     
            userSessionManagerBean.setSelectedDay (dateOfFc);
        userSessionManagerBean.setProductionInfoDays(productionScheduleDays);
        
        }
      
        return null;
    }

    /**
     * @param loadProductionInfo the loadProductionInfo to set
     */
    public void setLoadProductionInfo(String loadProductionInfo) {
        this.loadProductionInfo = loadProductionInfo;
    }

}
